home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / pascal / lzwp13.zip / TEST_LZW.PAS < prev    next >
Pascal/Delphi Source File  |  1993-09-09  |  7KB  |  215 lines

  1. (*
  2. **   TEST_LZW.PAS      Copyright (C) 1992 by MarshallSoft Computing, Inc.
  3. **
  4. **   This program is used to compress, expand, and verify each specified
  5. **   file. It's purpose is for you to test the LZW4P library on your own
  6. **   files. Your files are never modified. However, you should NOT have a
  7. **   file named "XXX.XXX" or "YYY.YYY".  Compression ratios are printed
  8. **   for each file compressed. For example, to compress all files ending
  9. **   in *.PAS in your current directory, type:
  10. **
  11. **        TEST_LZW *.PAS
  12. *)
  13.  
  14.  
  15. program TEST_LZW;
  16. uses dos, crt, memory, rw_io, hex_io, lzw_errs, LZW4P;
  17.  
  18. type
  19.   String12 = String[12];
  20.   AllocMemoryType = function(Size : Word) : Pointer;
  21.   FreeMemoryType  = function(P : Pointer; Size : Word) : Integer;
  22.  
  23. Var
  24.   FileName     : String12;
  25.   InpFileName  : String12;
  26.   OutFileName  : String12;
  27.   Inp1FileName : String12;
  28.   Inp2FileName : String12;
  29.   MemoryP      : Pointer;
  30.   AllocMemoryP : Pointer;
  31.   FreeMemoryP  : Pointer;
  32.   ReaderP      : Pointer;
  33.   WriterP      : Pointer;
  34.   Size         : Integer;
  35.   Code         : Integer;
  36.   i, x         : Integer;
  37.   DirInfo      : SearchRec;
  38.   F1, F2       : file;
  39.   Buffer1      : array [1..1024] of Byte;
  40.   Buffer2      : array [1..1024] of Byte;
  41.   NumRead1     : Integer;
  42.   NumRead2     : Integer;
  43.   Index        : LongInt;
  44.   Ratio        : Real;
  45.   ReaderCnt    : Real;
  46.   WriterCnt    : Real;
  47.   Count        : Integer;
  48.   BitCode      : Integer;
  49. begin
  50.   (* get file specs *)
  51.   if (ParamCount <> 1) and (ParamCount <> 2) then
  52.     begin
  53.       writeln('Usage: TEST_LZW <filespec>');
  54.       halt;
  55.     end;
  56.   (* sign on *)
  57.   writeln('TEST_LZW 1.1: Type any key to abort...');
  58.   writeln;
  59.   Count := 0;
  60.   BitCode := 0;
  61.   (* get pointers *)
  62.   AllocMemoryP := @AllocMemory;
  63.   FreeMemoryP  := @FreeMemory;
  64.   ReaderP := @Reader;
  65.   WriterP := @Writer;
  66.   (* Initialize LZW *)
  67.   if ParamCount = 2 then Val(ParamStr(2),BitCode,Code)
  68.   else BitCode := 14;
  69. writeln('BitCode=',BitCode);
  70.   Code := InitLZW(AllocMemoryP,BitCode);
  71.   if Code < 0 then
  72.     begin
  73.       SayError(Code);
  74.       Halt;
  75.     end;
  76.   writeln;
  77.   (* consider each file in FileSpec *)
  78.   FindFirst(ParamStr(1),0,DirInfo);
  79.   while DosError = 0 do
  80.   begin (* while *)
  81.     FileName := DirInfo.Name;
  82.     (*writeln('<',FileName,'>');*)
  83.     if (FileName<>'XXX.XXX') and (FileName<>'YYY.YYY') then
  84.       begin (* process file *)
  85.         if KeyPressed then
  86.           begin
  87.             writeln;
  88.             writeln('Aborted by USER');
  89.             Halt;
  90.           end;
  91.         Count := Count + 1;
  92.         InpFileName := FileName;
  93.         OutFileName := 'XXX.XXX';
  94.         (***** COMPRESSION *****)
  95.         (* open input file for compress *)
  96.         Code := ReaderOpen(InpFileName);
  97.         if Code <> 0 then
  98.           begin
  99.             writeln('Cannot open ',InpFileName,' for input. IOResult = ',Code);
  100.             halt;
  101.           end;
  102.         (* open output *)
  103.         Code := WriterOpen(OutFileName);
  104.         if Code <> 0 then
  105.           begin
  106.             writeln('Cannot open ',OutFileName,' for output. IOResult = ',Code);
  107.             halt;
  108.           end;
  109.         (* compress *)
  110.         write('COMPRESSING ',FileName:12,' ');
  111.         Code := Compress(ReaderP,WriterP);
  112.         if Code < 0 then
  113.           begin
  114.             SayError(Code);
  115.           end;
  116.         (* report compression ratio *)
  117.         if ReaderCount > 0 then
  118.           begin
  119.             ReaderCnt := ReaderCount;
  120.             WriterCnt := WriterCount;
  121.             Ratio := WriterCnt / ReaderCnt;
  122.             writeln('OK',Ratio:6:2);
  123.           end
  124.         else writeln('???');
  125.         (* close input & output *)
  126.         Code := ReaderClose;
  127.         Code := WriterClose;
  128.         (***** EXPANSION *****)
  129.         InpFileName := 'XXX.XXX';
  130.         OutFileName := 'YYY.YYY';
  131.         (* open input file for expansion *)
  132.         Code := ReaderOpen(InpFileName);
  133.         if Code <> 0 then
  134.           begin
  135.             writeln('Cannot open ',InpFileName,' for input. IOResult = ',Code);
  136.             halt;
  137.           end;
  138.         (* open output *)
  139.         Code := WriterOpen(OutFileName);
  140.         if Code <> 0 then
  141.           begin
  142.             writeln('Cannot open ',OutFileName,' for output. IOResult = ',Code);
  143.             halt;
  144.           end;
  145.         (* expand *)
  146.         write('  EXPANDING ',FileName:12,' ');
  147.         Code := Expand(ReaderP,WriterP);
  148.         if Code < 0 then
  149.           begin
  150.             SayError(Code);
  151.           end;
  152.         (* close input & output *)
  153.         Code := ReaderClose;
  154.         Code := WriterClose;
  155.         writeln('OK');
  156.         (*** COMPARING ***)
  157.         Inp1FileName := DirInfo.Name;
  158.         Inp2FileName := 'YYY.YYY';
  159.         (* open 1st input *)
  160.         Assign(F1,Inp1FileName);
  161. {$I-}
  162.         Reset(F1,1);
  163. {$I+}
  164.         if IOResult <> 0 then
  165.           begin
  166.             writeln('Cannot open ',Inp1FileName,' for input. IOResult = ',IOResult);
  167.             halt;
  168.           end;
  169.         (* open 2nd input *)
  170.         Assign(F2,Inp2FileName);
  171. {$I-}
  172.         Reset(F2,1);
  173. {$I+}
  174.         if IOResult <> 0 then
  175.           begin
  176.             writeln('Cannot open ',Inp2FileName,' for input. IOResult = ',IOResult);
  177.             halt;
  178.           end;
  179.         (* compare file byte for byte *)
  180.         write('  COMPARING ',FileName:12,' ');
  181.         Index := 0;
  182.         repeat
  183.           (* input 1st buffer *)
  184.           BlockRead(F1,Buffer1,Sizeof(Buffer1),NumRead1);
  185.           BlockRead(F2,Buffer2,Sizeof(Buffer2),NumRead2);
  186.           if NumRead1 <> NumRead2 then
  187.             begin
  188.               writeln('Error comparing files');
  189.               Halt;
  190.             end;
  191.           for i:= 1 to NumRead1 do
  192.             begin
  193.               Index := Index + 1;
  194.               if Buffer1[i] <> Buffer2[i] then
  195.                 begin
  196.                   writeln('Mismatch: Index=',Index,',Byte1=');
  197.                   WriteHexByte(Buffer1[i]);
  198.                   writeln(',Byte2=');
  199.                   WriteHexByte(Buffer2[i]);
  200.                   Halt;
  201.                 end;
  202.             end;
  203.         until (NumRead1=0) or (NumRead2=0);
  204.         writeln('OK');
  205.         writeln;
  206.         close(F1);
  207.         close(F2);
  208.       end; (* process file *)
  209.     (* get next filename *)
  210.     FindNext(DirInfo);
  211.   end; (* while *)
  212.   (* Terminate LZW *)
  213.   writeln(Count,' files processed.');
  214.   Code := TermLZW(FreeMemoryP);
  215. end.